package com.example.swaggerdemo.config;

import springfox.documentation.builders.HttpAuthenticationBuilder;
import springfox.documentation.builders.OpenIdConnectSchemeBuilder;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.SecurityScheme;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.Map;

public enum SwaggerSecuritySchemeTypeEnum {
	BASIC_AUTH {
		@Override
		public SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig) {
			return new HttpAuthenticationBuilder().name(name).scheme("basic").build();
		}
	},
	BEARER_AUTH {
		@Override
		public SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig) {
			String bearerFormat = "JWT";
			if (securitySchemeConfig.getExtensions().containsKey("bearerFormat")) {
				bearerFormat = securitySchemeConfig.getExtensions().get("bearerFormat");
			}
			return new HttpAuthenticationBuilder().name(name).scheme("bearer").bearerFormat(bearerFormat).build();
		}
	},
	OPEN_ID {
		@Override
		public SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig) {
			String openIdConnectUrl = "";
			if (securitySchemeConfig.getExtensions().containsKey("openIdConnectUrl")) {
				openIdConnectUrl = securitySchemeConfig.getExtensions().get("openIdConnectUrl");
			}
			return new OpenIdConnectSchemeBuilder().name(name).openIdConnectUrl(openIdConnectUrl)
					.description(securitySchemeConfig.getDescription())
					.extensions(securitySchemeConfig.getVendorExtensions()).build();
		}
	},
	OAUTH2 {
		@Override
		public SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig) {
			throw new NotImplementedException();
		}
	},
	API_KEY {
		@Override
		public SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig) {
			Map<String, String> extensions = securitySchemeConfig.getExtensions();
			String keyName = name;
			if (extensions.containsKey("keyName")) {
				keyName = extensions.get("keyName");
			}
			String passAs = "header";
			if (extensions.containsKey("passAs")) {
				passAs = extensions.get("passAs");
			}
			return new ApiKey(name, keyName, passAs);
		}
	};

	public abstract SecurityScheme getSecurityScheme(String name, SwaggerConfigProperties.Scheme securitySchemeConfig);
}
